import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const message 	= $Lazy.file(()=>import('@antd/message'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms: [
			{ label: '标本架',		name: 'sf_code',	id:'sf_code',
			onValue:({value})=>{
				const { forms } = this.state
				forms[0].btn.disabled = !Boolean(value)
			},
			onPressEnter: value => {
				if(value){
					this.getplane(value)
				}
			},
			btn:{ label:'领取', disabled:true, code:'F1', onClick: value=>this.getplane(value) } },
			{ label: '标本架编号',	name: 'sf_number',	 },
			{ label: '科室',		name: 'project_name',	type:'select',	loading:true,	idStr:'name' },
		],

	}
	
	componentDidMount(){
		this.getSelect()
		
		this.fetch()
	}
	// 领取标本架
	getplane = (value) => {
		$http.submit(null,'shelf/autouse',{ param:{ sf_code:value }}).then(data=>{
			message.then(f=>f.success(`操作成功`))
			this.fetch()
			this.searchFormRef.reset(['sf_code'] )
			setTimeout(()=>document.getElementById('sf_code')?.querySelector('.ant-input.ant-input-sm.ant-input-borderless')?.focus(), 700);		// 标本架领取成功，光标聚焦 2021/8/18 --马婷
		})
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 科室
			$fn.setRow( forms, 'project_name', await f.getCache({ api:'ke_shi' }) )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'shelf/myshelf')
	// 表单
	cols = [
		{ title: '标本架编号', 		dataIndex: 'sf_number', 		...$fn.table.exp_num },
		{ title: '标本架代码', 		dataIndex: 'sf_code', 			...$fn.table.exp_num },
		{ title: '科室', 			dataIndex: 'project_name', 		width:100 },
		{ title: '容量', 			dataIndex: 'total_capacity', 	...$fn.table.num },
		{ title: '已使用量', 		dataIndex: 'use_capacity', 		...$fn.table.num },
		{ title: '样本管数', 		dataIndex: 'tube', 				...$fn.table.num },
		TableControls.controls.call(this,{ width: 80 }),
	]
	
	// 表格操作按钮
	table_controls = (text,{sf_number,project_name}) => {
		return [
			// 上架完成
			TableControls.click.call(this, '上架完成', ()=>{
				$http.submit(null,'shelf/upshelfadded',{ param:{sf_number}, loadingText:`上架中`}).then(data=>{
					message.then(f=>f.success(`操作成功`))
					this.fetch()
					this.props.onRefresh({sf_number,project_name})
				})
			} )
		]
	}
		
	render(){
		return (
			<>	
				{/* 搜索 */}
				{ SearchForm.call(this, { noRefresh:true }) }
				{/* 表格 */}
				{ Table.call(this, { type: 1, onRow: rows => this.props.onRefresh(rows) }) }
			</>
		)
	}
}

export default Index